#import <Foundation/Foundation.h>
#import "GamePeer.h"


@class GameServer, TCPServer;

@protocol GameServerDelegate <NSObject>
- (void) gameServer:(GameServer*)server gamePeerDidDisconnect:(GamePeer*)peer;
@optional
- (void) gameServerDidStartAdvertisingToClients:(GameServer*)server;
- (void) gameServerWillStopAdvertisingToClients:(GameServer*)server;
- (void) gameServer:(GameServer*)server gamePeerDidConnect:(GamePeer*)peer;
@end

/*
This class implements the server side of a multiplayer game.
It can be connected to one or more clients which are represented as instances of the GamePeer class.
You can then communicate with the clients using either TCP (for reliability) or UDP (for speed) protocols.
The server can be registered for automatic discovery by GameClients on the local network using Bonjour.
UDP will be used instead of TCP if "immediate" is YES
*/

@class TCPServerConnection;
@class TCPServer;

@interface GameServer : NSObject
{
@private
	id<GameServerDelegate>		_delegate;
	NSUInteger					_delegateMethods;
    
	TCPServer*					_server;
    
	BOOL						_advertising;
    NSMutableSet*               pendingGamePeers;
}

@property(readonly) UInt16 port;
@property(assign) id<GameServerDelegate> delegate;
@property(readonly, getter=isAdvertising) BOOL advertising;

/* This function used to take a name and plist argument. I found it redundant to keep the information here.
It is also necessary for the GameClient, so it was moved up into the DataManager singleton object that
manages both the client and server. */
- (id) initWithPort:(UInt16)port;

/* These functions provide the basis for the zero-config networking. They have been further abstracted for use
with this program. To start networking, use the DataManager's functions (which start both client & server). 
It should not be necessary to call these separately. */
- (BOOL) startAdvertisingToClientsWithIdentifier:(NSString*)identifier; 
- (void) stopAdvertisingToClients;

- (void) server:(TCPServer*)server didOpenConnection:(TCPServerConnection*)connection;
- (void) server:(TCPServer*)server didCloseConnection:(TCPServerConnection*)connection;

- (void)gamePeerDidFailConnecting:(GamePeer*)p;
- (void)gamePeerDidConnect:(GamePeer*)p;
- (void)gamePeerDidDisconnect:(GamePeer*)p;
@end
